/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 17:49:36
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.cmcc.online.smsapi.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.cmcc.online.smsapi.sdk.Iservice.XyCallBack;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.entity.CountSceneManager;
import cn.cmcc.online.smsapi.sdk.db.entity.Resdownload;
import cn.cmcc.online.smsapi.sdk.db.entity.ResdownloadManager;
import cn.cmcc.online.smsapi.sdk.db.entity.SceneRule;
import cn.cmcc.online.smsapi.sdk.db.entity.SceneRuleManager;
import cn.cmcc.online.smsapi.sdk.db.entity.Sceneconfig;
import cn.cmcc.online.smsapi.sdk.db.entity.SceneconfigManager;
import cn.cmcc.online.smsapi.sdk.db.entity.SysParamEntityManager;
import cn.cmcc.online.smsapi.sdk.db.entity.XmlResdownloadManager;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.iccid.IccidLocationUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.NetUtil;
import cn.cmcc.online.smsapi.sdk.net.util.ServerResposeParseUtil;
import cn.cmcc.online.smsapi.sdk.net.util.ServerUtil;
import cn.cmcc.online.smsapi.sdk.ui.popu.util.ViewUtil;
/* SDK-509 huangzhiqiang 20160627 start */
import java.util.Set;
import java.util.HashSet;
/* SDK-509 huangzhiqiang 20160627 end */

public class SceneconfigUtil {

    public static SceneRule querySceneRuleByCondition(List<SceneRule> list) {
        if (list != null && !list.isEmpty()) {
            int size = list.size();
            if (size > 0) {
                SceneRule sceneRule = list.get(0);
                return sceneRule;
            }
            return null;
        } else {
            return null;
        }
    }

    public static void updateData() {
    	/*SDK-499 hwengao 20160620 start*/
        if (!NetUtil.isEnhance()) {
            return;
        }
        /*SDK-499 hwengao 20160620 end*/

          /* SDK-632 hewengao 20161009 start*/
//        long LastSceneConfigUpdateTime = SysParamEntityManager.getLongParam("LastSceneConfigUpdate", 0, Constant.getContext());
//        if (System.currentTimeMillis() > LastSceneConfigUpdateTime + DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_SCENE_CONFIG_UPDATE_CYCLE, Constant.sceneConfigupdatePrio)) {// 查询情景或升级
            //获取周期时间
            long periodTime = DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_SCENE_RULE_UPDATE_CYCLE, Constant.sceneConfigupdatePrio);
            List<Sceneconfig> list = null;
            list = SceneconfigManager.queryAllSceneconfig(SceneconfigManager.SCENE_TYPE_POPUP,periodTime);
            insertOrUpdateSceneConfigAndRequestScenceConfig(list, SceneconfigManager.SCENE_TYPE_POPUP, true);

            list = SceneconfigManager.queryAllSceneconfig(SceneconfigManager.SCENE_TYPE_BUBBLE,periodTime);
            insertOrUpdateSceneConfigAndRequestScenceConfig(list, SceneconfigManager.SCENE_TYPE_BUBBLE, true);

            // Constant.setLastSceneConfigUpdateTime(Constant.getContext());
            //SysParamEntityManager.setParam("LastSceneConfigUpdate", System.currentTimeMillis() + "");

//        }

//        long LastSceneRuleUpdateTime = SysParamEntityManager.getLongParam("LastSceneRuleUpdate", 0, Constant.getContext());
//        if (System.currentTimeMillis() > LastSceneRuleUpdateTime + DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_SCENE_RULE_UPDATE_CYCLE, Constant.SceneRuleUpdatePrio)) { // 检测情景规则升级
            //获取周期时间
            periodTime = DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_SCENE_RULE_UPDATE_CYCLE, Constant.SceneRuleUpdatePrio);
            List<SceneRule> listRule = SceneRuleManager.queryAllSceneRule(SceneconfigManager.SCENE_TYPE_POPUP,periodTime);
            requestQuerySceneRuleRequest(listRule, SceneconfigManager.SCENE_TYPE_POPUP);

            listRule = SceneRuleManager.queryAllSceneRule(SceneconfigManager.SCENE_TYPE_BUBBLE,periodTime);
            requestQuerySceneRuleRequest(listRule, SceneconfigManager.SCENE_TYPE_BUBBLE);

            // Constant.setLastSceneRuleUpdateTime(Constant.getContext());
//            SysParamEntityManager.setParam("LastSceneRuleUpdate", System.currentTimeMillis() + "");
//        }
         /* SDK-632 hewengao 20161009 end*/
    }

    public static SceneRule getSceneRule(final String titleNo, int sceneType) {

        boolean hasImportData = SysParamEntityManager.getBooleanParam(Constant.getContext(), Constant.HAS_IMPORT_DRAWABLE_DATA);
        Sceneconfig sceneconfig = SceneconfigManager.querySceneconfigBySceneId(titleNo, sceneType);
//        LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + "hasImportData: " + hasImportData);
        if (sceneconfig != null || hasImportData) {
            List<SceneRule> sceneRuleList = null;
            if (ViewUtil.getChannelType() == 7) {
                sceneRuleList = SceneRuleManager.querySceneRuleBySceneId(titleNo, sceneType, true);
            } else {
                sceneRuleList = SceneRuleManager.querySceneRuleBySceneId(titleNo, sceneType);
            }
            if (sceneRuleList != null && !sceneRuleList.isEmpty()) {
//                LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + "sceneRuleList is not Empty");
                SceneRule item = querySceneRuleByCondition(sceneRuleList);
                if (item != null) {

                    String urls = "";
                    if (item != null) {
                        urls = item.res_urls;
                    }

                    if (item.isDownload == 1 || isResDownloaded(urls)) {
                        if (LogManager.debug) {
//                            LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + "全部下载了. urls: " + urls);
                        }

                        if (item != null && item.isDownload == 0) {
                            SceneRuleManager.updateIsdownload(item, 1);
                        }
                        return item;
                    } else {

                        List<String> list = ResdownloadManager.getUrls(urls);
                        if (list != null && !list.isEmpty()) {
                            int size = list.size();
                            for (int i = 0; i < size; i++) {
                                String item1 = list.get(i);
                                ZipPackageDownloaderQueue.addNeedPriorDownUrlData(item1);
                                if (LogManager.debug) {
//                                    LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + " download urls: " + item1);
                                }

                            }
                            ZipPackageDownloaderQueue.runDownResQueue(false);

                        }

                        if (LogManager.debug) {
//                            LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + "urls---还没有全部下载了");
                        }

                        return null;
                    }

                } else { // 根据条件找不到对应的，不弹窗，也不请求。
                    if (LogManager.debug) {
//                        LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + " 根据条件找不到对应的，不弹窗，也不请求");
                    }

                    return null;
                }

            } else {

//                LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + "情景不存在。需要获取情景配置数据。");

                Sceneconfig item = new Sceneconfig();
                item.sceneVersion = "-1";
                item.sceneId = titleNo;
                List<Sceneconfig> list = new ArrayList<Sceneconfig>();
                list.add(item);
                item.isDownload = SceneconfigManager.NOTFINISH;
                // 加入情景队列
                SceneconfigManager.insertOrUpdate(item, sceneType);
                // 请求情景
                insertOrUpdateSceneConfigAndRequestScenceConfig(list, sceneType, false);
                return null;

            }
        } else {

            Sceneconfig item = new Sceneconfig();
            item.sceneVersion = "-1";
            item.sceneId = titleNo;
            item.isDownload = SceneconfigManager.NOTFINISH;
            if (XyUtil.checkNetWork(Constant.getContext()) == 0) { // wifi，直接下载
//                LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + " 加入情景队列 wifi，直接下载");
                List<Sceneconfig> list = new ArrayList<Sceneconfig>();
                list.add(item);
                // 加入情景队列

                SceneconfigManager.insertOrUpdate(item, sceneType);
                // 请求情景
                insertOrUpdateSceneConfigAndRequestScenceConfig(list, sceneType, false);
            } else {// 非wifi，插入队列
                    // 加入情景队列
//                LogManager.i("SceneconfigUtil", "titleNo =" + titleNo + " 加入情景队列");
                SceneconfigManager.insertOrUpdate(item, sceneType);
            }

        }
        return null;

    }

    public static void handleSceneconfig(List<Sceneconfig> list, final int sceneType) {
        try {
            if (list != null & !list.isEmpty()) {
                int size = list.size();
                for (int i = 0; i < size; i++) {
                    Sceneconfig item = list.get(i);
                    if (item == null) {
                        continue;
                    }
                    item.isDownload = SceneconfigManager.FINISH;
                    // 删掉情景请求队列里的记录
                    SceneRuleManager.deleteSceneRuleById(item.sceneId, sceneType);
                    SceneconfigManager.insertOrUpdate(item, sceneType);
                    /* SDK-509 huangzhiqiang 20160627 start */
                    handlerSceneRule(item.sceneRuleList, sceneType);
                    /* SDK-509 huangzhiqiang 20160627 end */
                }
            }
        } catch (Throwable e) {
            // TODO: handle Throwable
        }

    }

    public static void handleSceneUrllist(final List<Sceneconfig> orglist, ArrayList<String> sceneUrllist, final int sceneType) {
        try {
            if (sceneUrllist != null & !sceneUrllist.isEmpty()) {

                int size = sceneUrllist.size();
                for (int i = 0; i < size; i++) {
                    String url = sceneUrllist.get(i);
                    if (!XmlResdownloadManager.isXmlResDownloaded(url)) {
                        // 假如没有在下载，则要加到队列里下载
                        XmlResdownloadManager.insertOrUpdate("", url, XmlResdownloadManager.NOTFINISH, 0, sceneType);
                        XmlDownloaderQueue.addNeedPriorDownUrlData(url);
                    }

                }
                XmlDownloaderQueue.runDownResQueue(false);

            }
        } catch (Throwable e) {
            // TODO: handle Throwable
        }

    }

    /* SDK-509 huangzhiqiang 20160627 start */
    private static final Map<String, Long> LAST_REQUEST_SCENE_TIME = new HashMap<String, Long>();

    private static boolean addLastRequestSceneTime(String scenceInfo) {
        synchronized (LAST_REQUEST_SCENE_TIME) {
            Long time = LAST_REQUEST_SCENE_TIME.get(scenceInfo);
            if (time == null || System.currentTimeMillis() > (time + Constant.MINUTE)) {
                LAST_REQUEST_SCENE_TIME.put(scenceInfo, System.currentTimeMillis());
                return true;
            }
            return false;
        }
    }

    private static void removeLastRequestSceneTime(List<Sceneconfig> scenceConfigs) {
        if (scenceConfigs == null || scenceConfigs.isEmpty()) {
            return;
        }
        try {
            for (Sceneconfig sceneConfig : scenceConfigs) {
                synchronized (LAST_REQUEST_SCENE_TIME) {
                    Long time = LAST_REQUEST_SCENE_TIME.get(sceneConfig.toString());
                    if (time != null && (System.currentTimeMillis() - time) > Constant.MINUTE) {
                        LAST_REQUEST_SCENE_TIME.remove(sceneConfig.toString());
                    }
                }
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "SceneconfigUtil removeLastRequestSceneTime error:", e);
        }
    }

    public static void insertOrUpdateSceneConfigAndRequestScenceConfig(List<Sceneconfig> scenceConfigs, int sceneType, boolean isClear) {
        distinctRequestScenceAndRecordLastRequestSceneTime(scenceConfigs);

        if (scenceConfigs == null || scenceConfigs.isEmpty()) {
            return;
        }

//        SceneconfigManager.insertOrUpdateSceneConfig(scenceConfigs, sceneType);

        int len = 0;
        int cnt = 0;
        while ((len = scenceConfigs.size()) > 0) {
            // 最多10个一批
            List<Sceneconfig> requestScenceConfigs = new ArrayList<Sceneconfig>();
            requestScenceConfigs.addAll(scenceConfigs.subList(0, len > 25 ? 25 : len));
            doRequestScenceConfig(requestScenceConfigs, sceneType, isClear);
            scenceConfigs.removeAll(requestScenceConfigs);
            cnt++;
            if(cnt > 10){
                break;
            }
        }
    }

    /**
     * 过滤掉重复请求的情景并记录有效情景的请求时间到内存
     * 
     * @param scenceconfigs
     */
    private static void distinctRequestScenceAndRecordLastRequestSceneTime(List<Sceneconfig> scenceconfigs) {
        if (scenceconfigs == null || scenceconfigs.isEmpty()) {
            return;
        }
        try {
            for (int i = 0; i < scenceconfigs.size(); i++) {
                String scenceInfo = scenceconfigs.get(i).toString();
                // 无法添加到最后请求时间Map代表没有超过请求周期导致添加失败，即属于重复请求
                if (!addLastRequestSceneTime(scenceInfo)) {
                    scenceconfigs.remove(i);
                    i--;
                }
            }
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "SceneconfigUtil distinctRequestScenceAndRecordLastRequestSceneTime error:", e);
        }
    }

    private static void doRequestScenceConfig(final List<Sceneconfig> scenceConfigs, final int sceneType,
            final boolean isClear) {
        try {
            /* SDK-499 hwengao 20160620 start */
            if (!NetUtil.isEnhance() || !NetUtil.checkAccessNetWork(2)) {
                return;
            }
            /* SDK-499 hwengao 20160620 end */

            /* SDK-504 huangzhiqiang 20160623 start */
            String dataString = ServerUtil.getQuerySceneRequest(scenceConfigs);
            if (StringUtils.isNull(dataString)) {
                return;
            }

            XyCallBack callback = new XyCallBack() {
                @Override
                public void execute(Object... obj) {
                    try {
                        if (obj == null || obj.length != 2 || !"0".equals(String.valueOf(obj[0]))) {
                            return;
                        }
                        Set<String> sceneIds = getSceneIds(scenceConfigs);
                        if (sceneIds != null && !sceneIds.isEmpty()) {
                            SceneconfigManager.updateIsCheckBySceneIdAndSceneType(sceneIds, sceneType,
                                    SceneconfigManager.FINISH);
                            if (isClear) {
                                SceneconfigManager.setIsUseAndResetUseCount(sceneIds);
                            }

                            // 情景资源请求成功后从Map中清除，因为这些情景不会再次请求了，也不需要通过Map来防止重复请求
                            removeLastRequestSceneTime(scenceConfigs);
                        }

                        String response = obj[1].toString();
                        HashMap<String, Object> result = ServerResposeParseUtil.parseQuerySceneRecordResponse(response);
                        if (result == null) {
                            return;
                        }

                        ArrayList<Sceneconfig> list = (ArrayList<Sceneconfig>) result.get("sceneconfigList");
//                        PrintTestLogUtil.printTestLog("SceneconfigUtil", "处理回来的 sceneconfigList=" + list);
                        // 报文回来后，处理list
                        handleSceneconfig(list, sceneType);

                        ArrayList<String> sceneUrllist = (ArrayList<String>) result.get("sceneUrllist");
//                        PrintTestLogUtil.printTestLog("SceneconfigUtil", "处理回来的 sceneUrllist=" + sceneUrllist);
                        // 报文回来后，处理list
                        handleSceneUrllist(scenceConfigs, sceneUrllist, sceneType);
                    } catch (Throwable e) {
                        LogManager.e(Constant.TAG,
                                "SceneconfigUtil doRequestScenceConfig callback handle error:", e);
                    }finally {
                         /* SDK-632 hewengao 20161010 start*/
                         SceneconfigManager.updateSceneConfigUpdateTime(scenceConfigs);
                         /* SDK-632 hewengao 20161010 end*/
                    }
                }
            };

            NetUtil.executeHttpRequest(0, sceneType, dataString, callback,
                    NetUtil.getPopupServiceUrl() + NetUtil.QuerySceneRequest, true);
            /* SDK-504 huangzhiqiang 20160623 end */
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "SceneconfigUtil doRequestScenceConfig error", e);
        }
    }

    private static Set<String> getSceneIds(List<Sceneconfig> sceneConfigs) {
        if (sceneConfigs == null || sceneConfigs.isEmpty()) {
            return null;
        }
        Set<String> sceneIds = new HashSet<String>();
        for (Sceneconfig sceneConfig : sceneConfigs) {
            sceneIds.add(sceneConfig.sceneId);
        }
        return sceneIds;
    }

    public static void requestQuerySceneRuleRequest(List<SceneRule> sceneRules, int sceneType) {
        if (sceneRules == null || sceneRules.isEmpty()) {
            return;
        }
        /* SDK-632 hewengao 20161009 start*/
        int len = 0;
        int cnt = 0;
        while ((len = sceneRules.size()) > 0) {
            // 最多25个一批
            List<SceneRule> requestSceneRules = new ArrayList<SceneRule>();
            requestSceneRules.addAll(sceneRules.subList(0, len > 25 ? 25 : len));
            doRequestQuerySceneRuleRequest(requestSceneRules, sceneType);
            sceneRules.removeAll(requestSceneRules);
            cnt++;
            if(cnt > 10){
                //一次最多处理10批
                break;
            }
        }
        /* SDK-632 hewengao 20161009 end*/
    }

    private static void doRequestQuerySceneRuleRequest(final List<SceneRule> list, final int sceneType) {
        try {
            /* SDK-499 hewengao 20160620 start */
            if (!NetUtil.isEnhance() || !NetUtil.checkAccessNetWork(2)) {
                return;
            }
            /* SDK-499 hewengao 20160620 end */

            String dataString = ServerUtil.getQuerySceneRuleRequest(list);
            if (StringUtils.isNull(dataString)) {
                return;
            }

            XyCallBack callback = new XyCallBack() {
                @Override
                public void execute(Object... obj) {
                    try {
                        if (obj == null || obj.length != 2 || !"0".equals(String.valueOf(obj[0]))) {
                            return;
                        }
                        String response = obj[1].toString();
                        if (LogManager.debug) {
                            LogManager.d("showPopuMessage",
                                    "SceneconfigUtil doRequestQuerySceneRuleRequest response :", response);
                        }
                        List<SceneRule> sceneRuleList = ServerResposeParseUtil.parseQuerySceneRuleResponse(response);
                        handlerSceneRule(sceneRuleList, sceneType);
                    } catch (Throwable e) {
                        LogManager.e(Constant.TAG,
                                "SceneconfigUtil doRequestQuerySceneRuleRequest callBack error: ", e);
                    }finally {
                         /* SDK-632 hewengao 20161009 start*/
                        SceneRuleManager.updateSceneRuleUpdateTime(list);
                         /* SDK-632 hewengao 20161009 end*/
                    }
                }
            };

            /* SDK-504 huangzhiqiang 20160623 start */
            NetUtil.executeHttpRequest(0, sceneType, dataString, callback, NetUtil.getPopupServiceUrl(), true);
            /* SDK-504 huangzhiqiang 20160623 end */
        } catch (Throwable e) {
            LogManager.w(Constant.TAG, "SceneconfigUtil doRequestQuerySceneRuleRequest error: ", e);
        }
    }

    private static void handlerSceneRule(List<SceneRule> sceneRuleList, int sceneType) {
        if (sceneRuleList == null || sceneRuleList.isEmpty()) {
            return;
        }
        for (SceneRule sceneRule : sceneRuleList) {
            if (sceneRule == null) {
                continue;
            }
            SceneRuleManager.insertOrUpdate(sceneRule, sceneType);
            insertOrUpdateResDownloadInfoAndExecuteResDownload(sceneRule.scene_id, sceneRule.res_urls);
        }
    }

    private static void insertOrUpdateResDownloadInfoAndExecuteResDownload(String sceneId, String resUrls) {
        List<String> urls = getUrls(resUrls);
        if (urls == null || urls.isEmpty()) {
            return;
        }
        for (String url : urls) {
            if (ResdownloadManager.isResDownloaded(url)) {
                continue;
            }
            Resdownload resdownload = new Resdownload();
            resdownload.pos = 0;
            resdownload.scene_id = sceneId;
            resdownload.status = 0;
            resdownload.url = url;
            ResdownloadManager.insertOrUpdate(resdownload);
            ZipPackageDownloaderQueue.addNeedPriorDownUrlData(url);
        }
        ZipPackageDownloaderQueue.runDownResQueue(false);
    }
    /* SDK-509 huangzhiqiang 20160627 end */

    public static boolean isResDownloaded(String urls) {
        if (!StringUtils.isNull(urls)) {
            urls = urls.replaceAll("；", ";");
            String str[] = urls.split(";");
            List list = java.util.Arrays.asList(str);
            if (list != null && !list.isEmpty()) {
                int size = list.size();
                for (int i = 0; i < size; i++) {
                    String url = (String) list.get(i);
                    if (!ResdownloadManager.isResDownloaded(url)) {
                        if (LogManager.debug) {
                            LogManager.d("SceneconfigUtil",  url,"还没下载");
                        }
                        return false;
                    }
                }
                return true;
            }
        } else {
            return true;
        }
        return false;
    }

    public static List<String> getUrls(String urls) {
        if (!StringUtils.isNull(urls)) {

            urls = urls.replaceAll("；", ";");
            String str[] = urls.split(";");
            List list = java.util.Arrays.asList(str);
            return list;
        }
        return null;
    }

    public static void postqueryIccidScene() {
        try {
            boolean ispost = false;
            Long PostCount = SysParamEntityManager.getLongParam("PostCount", 0, Constant.getContext());
            long time = SysParamEntityManager.getLongParam("LastPostIccidSceneTime", 0, Constant.getContext());
            if (time == 0) {
                SysParamEntityManager.setParam("LastPostIccidSceneTime", System.currentTimeMillis() + "");
                time = System.currentTimeMillis();
            }
            if (PostCount == 0) {
                ispost = System.currentTimeMillis() > (time + DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_FIRST_POST_ICCID_SCENE_COUNT_CYCLE, Constant.FirstpostqueryIccidScene));
            } else {
                ispost = System.currentTimeMillis() > (time + DexUtil.getUpdateCycleByType(OnlineUpdateCycleConfigInterface.TYPE_POST_ICCID_SCENE_COUNT_CYCLE, Constant.postqueryIccidScene));
            }
            // 测试
            // ispost = true;

            if (ispost) {
                List<Sceneconfig> list = CountSceneManager.queryAllSceneconfig();
                StringBuffer sb = new StringBuffer();
                if (list != null && !list.isEmpty()) {
                    for (int i = 0; i < list.size(); i++) {
                        sb.append(list.get(i).sceneId + "," + list.get(i).count + ";");
                    }

                    XyCallBack callback = new XyCallBack() {

                        @Override
                        public void execute(Object... obj) {
                            if (obj != null) {
                                String resutCode = obj[0].toString();

                                if (resutCode.equals("0") && obj.length == 2) {
                                    String response = obj[1].toString();

                                    boolean resultBack = ServerResposeParseUtil.parseQueryIccidSceneResponseResultCode(response);

                                    if (resultBack) {
                                        // Constant.setLastPostIccidSceneTime(Constant.getContext());
                                        SysParamEntityManager.setParam("LastPostIccidSceneTime", System.currentTimeMillis() + "");
                                        long count = SysParamEntityManager.getLongParam("PostCount", 0, Constant.getContext());
                                        SysParamEntityManager.setParam("PostCount", (count + 1) + "");
                                        // Constant.increasePostCount(Constant.getContext());
                                        CountSceneManager.deleteAll();
                                    }

                                }
                            }
                        }

                    };
                    if (NetUtil.isEnhance()) {
                        /* SDK-504 huangzhiqiang 20160623 start */
                        String dataString = ServerUtil.queryIccidSceneRequest(
                                StringUtils.getMD5(IccidLocationUtil.getICCID(Constant.getContext())), "1",
                                XyUtil.getImeiAndXinghao(Constant.getContext()), sb.toString());

                        if (!StringUtils.isNull(dataString)) {
                            NetUtil.executeLoginBeforeHttpRequest(dataString, "990005", callback,
                                    NetUtil.STATSERVICE_URL, true);
                        }
                        /* SDK-504 huangzhiqiang 20160623 end */
                    }
                }

            }
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "postqueryIccidScene: ", e);

        }

    }

}
